package com.tgy.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: tgy
 * @Date: 12/18/20 1:36 PM
 */
public class _46_全排列 {

    public List<List<Integer>> permute(int[] nums) {

        List<List<Integer>> rets = new ArrayList<List<Integer>>();
        if (nums == null || nums.length == 0) {

            return rets;
        }

        search(0,nums,rets,new int[nums.length]);

        return rets;
    }

    private void search(int deepth , int[] nums,List<List<Integer>> rets,int[] item) {

        if (deepth == nums.length) {

            rets.add(Arrays.stream(item).boxed().collect(Collectors.toList()));
            return;
        }

        for (int i = 0; i < nums.length; i++) {

            boolean flag = true;
            for (int j = 0; j < deepth; j++) {

                if (nums[i] == item[j]) {

                    flag = false;
                    break;
                }
            }

            if (flag) {

                item[deepth] = nums[i];
                search(deepth+1,nums,rets,item);
            }

        }

    }
}
